package com.elite.controller;

import com.elite.entity.FileGoods;
import com.elite.service.FileGoodsService;
import com.elite.service.FileService;
import com.elite.service.GoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

/**
 * Created by hbl on 2017/2/10.
 */
@RestController
@RequestMapping("/fileGoods")
public class FileGoodsController {
    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    @Autowired
    FileGoodsService fileGoodsService;
    @Autowired
    FileService fileService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public void delete(Integer goodsId){
//        goodsId搜索fileId
        List<FileGoods> fileGoodses =  fileGoodsService.selectAllByGoodsId(goodsId);

//        通过goodsid把file-goods里面的删除了
        fileGoodsService.deleteByGoodsId(goodsId);

        Iterator goodses = fileGoodses.iterator();
        while(goodses.hasNext()){
            FileGoods hasFile = (FileGoods) goodses.next();
            Integer fileId = hasFile.getFileId();
            LOGGER.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>>"+hasFile);
//            通过fileid来删除file文件的信息
            fileService.delete(fileId);
        }
//        通过goodsid来删除goods里面的信息
        goodsService.deleteByGoodsId(goodsId);
    }
}
